package com.athena.framework.url.info;

import cn.hutool.core.util.StrUtil;
import com.athena.framework.exception.UrlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

/**
 * 保存URL信息
 */
public class UrlInfoFactory {

    private final static Logger logger = LoggerFactory.getLogger(UrlInfoFactory.class);

    private static UrlInfoFactory urlInfoFactory;

    /**
     * url路径和key的映射
     */
    private static final Map<String, String> urlKeyMap = new ConcurrentHashMap();
    /**
     * 所有的URL信息
     */
    private static final Map<String, UrlInfo> urlMap = new ConcurrentHashMap();

    private UrlInfoFactory() {
    }

    /**
     * 获取UrlInfoFactory实例
     *
     * @return
     */
    public static UrlInfoFactory creatUrlInfoFactory() {
        if (urlInfoFactory == null) {
            urlInfoFactory = new UrlInfoFactory();
        }
        return urlInfoFactory;
    }

    /**
     * 添加url信息
     *
     * @param urlInfo
     */
    public void addUrlInfo(UrlInfo urlInfo) {
        if (StrUtil.isBlank(urlInfo.getKey())) {
            String msg = "url信息的key和数据不能为空或空显示为空白的字符串！！！";
            logger.error(msg);
            throw new UrlException(msg);
        }
        addUrlInfo(urlInfo.getKey(), urlInfo);
    }

    /**
     * 添加url信息
     *
     * @param key     key必须是唯一的
     * @param urlInfo
     */
    public void addUrlInfo(String key, UrlInfo urlInfo) {
        if (StrUtil.isBlank(key) || urlInfo == null) {
            String msg = "url信息的key和数据不能为空或空显示为空白的字符串！！！";
            logger.error(msg);
            throw new UrlException(msg);
        }

        //没有重复的url路径，就把url作为key，方便根据URL找到要执行的对象或路径
        if (urlInfo.getUrl() != null) {
            if (UrlInfoFactory.urlKeyMap.get(urlInfo.getKey()) == null) {
                UrlInfoFactory.urlKeyMap.put(urlInfo.getUrl(), key);
            } else {
                String msg = "url路径不是唯一的！！！";
                logger.error(msg);
                throw new UrlException(msg);
            }
        }

        //没有重复的key就可以添加
        if (UrlInfoFactory.urlMap.get(key) == null) {
            UrlInfoFactory.urlMap.put(key, urlInfo);
        } else {
            String msg = "url信息的key不是唯一的！！！";
            logger.error(msg);
            throw new UrlException(msg);
        }
    }

    /**
     * 根据key获取url信息
     *
     * @param key
     * @return
     */
    public UrlInfo getUrlInfoByKey(String key) {
        return UrlInfoFactory.urlMap.get(key);
    }

    /**
     * 根据url获取url信息
     *
     * @param url
     * @return
     */
    public UrlInfo getUrlInfoByUrl(String url) {
        String key = UrlInfoFactory.urlKeyMap.get(url);
        if (key == null) {
            return null;
        }
        return UrlInfoFactory.urlMap.get(key);
    }

    /**
     * 遍历所有的url信息
     * @param action
     */
    public void forEachUrlMap(BiConsumer<String, UrlInfo> action){
        UrlInfoFactory.urlMap.forEach(action);
    }
}
